Fix phpstan/phpstan#5865: Do-while loop broken by exception not supported#5469
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#5865: Do-while loop broken by exception not supported#5469phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…rted Detect void methods/functions that always terminate (throw/exit) during body analysis and suppress false "always true/false" loop condition reports when such calls appear in loop bodies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes phpstan/phpstan#5865
Summary
When a
void-typed method or function always throws an exception (but doesn't declareneveras its return type), PHPStan was unable to detect that calls to it would always terminate execution. This caused false positive "loop condition is always true" errors fordo-whileandwhileloops whose bodies called such methods.For example:
Approach
During method and function body analysis,
NodeScopeResolveralready tracks execution ends and whether the body always terminates (this is used byNeverRuleHelperto suggest theneverreturn type). This PR stores that "always terminates" information in a lookup map (alwaysTerminatingCallables) keyed by the fully-qualified callable name, then consults it infindEarlyTerminatingExprwhen processing:$this->alwaysThrows())self::staticAlwaysThrows())alwaysThrowsFunction())First-class callable syntax (e.g.
$this->method(...)) is explicitly excluded since it creates a closure rather than invoking the method.Limitation: This only works when the called method/function is analyzed before the caller (same file, declaration order). Cross-file calls won't benefit from this detection — those cases would require declaring
neveras the return type.Test plan
tests/PHPStan/Rules/Comparison/data/bug-5865.php— do-while loop regression tests covering instance methods, variable conditions,neverreturn type, static methods, and function callstests/PHPStan/Rules/Comparison/data/bug-5865-while.php— analogous while loop regression testsDoWhileLoopConstantConditionRuleTestandWhileLoopAlwaysTrueConditionRuleTest